home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / vbcc / declaration.c next >
C/C++ Source or Header  |  1995-11-18  |  47KB  |  1,223 lines

  1. #include "vbc.h"
  2.  
  3. #define PARAMETER 8
  4. #define OLDSTYLE 16
  5.  
  6. struct const_list *initialization(struct Typ *,int);
  7. int test_assignment(struct Typ *,np);
  8. int return_sc;
  9.  
  10. extern int float_used;
  11.  
  12. int settyp(int typnew, int typold)
  13. /* Unterroutine fuer declaration_specifiers()               */
  14. {
  15.     if(DEBUG&2) printf("settyp: new=%d old=%d\n",typnew,typold);
  16.     if(typold==LONG&&typnew==FLOAT) return(DOUBLE);
  17.     if(typold!=0&&typnew!=INT){error(47);return(typnew);}
  18.     if(typold==0&&typnew==INT) return(INT);
  19.     if(typold==0) return(typnew);
  20.     if(typold==SHORT||typold==LONG) return(typold);
  21.     error(48);
  22.     return(typnew);
  23. }
  24.  
  25. #define dsc if(storage_class) error(49); if(typ||type_qualifiers) error(50)
  26.  
  27. struct Typ *declaration_specifiers(void)
  28. /* Erzeugt neuen Typ und gibt Zeiger darauf zurueck,      */
  29. /* parst z.B. unsigned int, struct bla etc.               */
  30. /* evtl. muessen noch storage-classes uns strengere       */
  31. /* Pruefungen etc. eingebaut werden                       */
  32. {
  33.     int typ=0,type_qualifiers=0,notdone,storage_class;
  34.     char *merk,*imerk,sident[MAXI],sbuff[MAXI];
  35.     struct Typ *new=(struct Typ *)mymalloc(TYPS),*t,*ts;
  36.     struct struct_declaration *ssd,*p;
  37.     struct struct_identifier *si;
  38.     struct Var *v;
  39.     storage_class=0;
  40.     new->next=0; new->exact=0;
  41.     do{
  42.         merk=s;killsp();cpbez(buff);notdone=0;
  43.         if(DEBUG&2) printf("ts: %s\n",buff);
  44.         if(!strcmp("struct",buff)) notdone=STRUCT;
  45.         if(!strcmp("union",buff)) notdone=UNION;
  46.         if(notdone!=0){
  47.             killsp();
  48.             if(*s!='{'){
  49.                 cpbez(sident);
  50.                 p=find_struct(sident);
  51.                 if(p==0) {typ=notdone|UNCOMPLETE;
  52.                           new->exact=(void *)add_identifier(sident,strlen(sident));
  53.                 }else{
  54.                     new->exact=p;
  55.                     typ=new->flags=notdone;
  56.                 }
  57.                 killsp();
  58.             }else *sident=0;
  59.             if(*s=='{'){
  60.                 si=first_si[nesting];
  61.                 while(si){
  62.                     if(!strcmp(si->identifier,sident)) {error(13,sident);break;}
  63.                     si=si->next;
  64.                 }
  65.                 s++;
  66.                 killsp();
  67.                 ssd=(struct struct_declaration *)mymalloc(MAXSTRUCTSIZE);
  68.                 ssd->count=0;
  69.                 imerk=ident;
  70.                 ts=declaration_specifiers();
  71.                 while(*s!='}'&&ts){
  72.                     ident=sbuff;
  73.                     t=declarator(clone_typ(ts));
  74.                     killsp();
  75.                     if(*s==':'){
  76.                     /*  bitfields werden hier noch ignoriert    */
  77.                         np tree;
  78.                         if((ts->flags&15)!=INT) error(51);
  79.                         s++;killsp();tree=assignment_expression();
  80.                         if(type_expression(tree)){
  81.                             if(tree->flags!=CEXPR) error(52);
  82.                             if((tree->ntyp->flags&15)<CHAR||(tree->ntyp->flags&15)>LONG) error(52);
  83.                         }
  84.                         if(tree) free_expression(tree);
  85.                     }else{
  86.                         if(*ident==0) error(53);
  87.                     }
  88.                     if(type_uncomplete(t)){
  89.                         error(14,sbuff);
  90.                         freetyp(t);
  91.                         break;
  92.                     }
  93.                     if((t->flags&15)==FUNKT)
  94.                         error(15,sbuff);
  95.  
  96.                     if(*ident!=0){
  97.                         int i=ssd->count;
  98.                         while(--i>=0)
  99.                             if(!strcmp(ssd->sl[i].identifier,ident))
  100.                                 error(16,ident);
  101.                     }
  102.                     ssd->sl[ssd->count].styp=t;
  103.                     ssd->sl[ssd->count].identifier=add_identifier(ident,strlen(ident));
  104.                     ssd->count++;
  105.                     killsp();
  106.                     if(*s==',') {s++;killsp();continue;}
  107.                     if(*s!=';') error(54); else s++;
  108.                     killsp();
  109.                     if(*s!='}'){
  110.                         if(ts) freetyp(ts);
  111.                         ts=declaration_specifiers();killsp();
  112.                     }
  113.                 }
  114.                 if(ts) freetyp(ts);
  115.                 if(ssd->count==0) error(55);
  116.                 ident=imerk;
  117.                 new->exact=add_sd(ssd);
  118.                 typ=notdone;
  119.                 free(ssd);
  120.                 if(*s!='}') error(56); else s++;
  121.                 if(*sident!=0) add_struct_identifier(sident,new->exact);
  122.                 new->flags=notdone|type_qualifiers;
  123.             }
  124.             notdone=1;
  125.         }
  126.         if(!strcmp("enum",buff)){
  127.         /*  enumerations; die Namen werden leider noch ignoriert    */
  128.             killsp();notdone=1;
  129.             if(*s!='{'){cpbez(buff);killsp();}
  130.             if(*s=='{'){
  131.                 zlong val; struct Var *v; struct Typ *t;
  132.                 val=l2zl(0L);
  133.                 s++;killsp();
  134.                 while(*s!='}'){
  135.                     cpbez(sident);killsp();
  136.                     t=(struct Typ *)mymalloc(TYPS);
  137.                     t->flags=CONST|INT;
  138.                     t->next=0;
  139.                     if(find_var(sident,nesting)) error(17,sident);
  140.                     v=add_var(sident,t,AUTO,0); /*  AUTO hier klug? */
  141.                     if(*s=='='){
  142.                         s++;killsp();
  143.                         v->clist=initialization(v->vtyp,0);
  144.                         val=zi2zl(v->clist->val.vint);killsp();
  145.                     }else{
  146.                         v->clist=(struct const_list *)mymalloc(CLS);
  147.                         v->clist->val.vint=val;
  148.                         v->clist->next=v->clist->other=0;
  149.                         v->clist->tree=0;
  150.                     }
  151.                     vlong=l2zl(1L);val=zladd(val,vlong);
  152.                     v->vtyp->flags=CONST|ENUM;
  153.                     if(*s=='}') break;
  154.                     if(*s==',') s++; else error(57);
  155.                     killsp();
  156.                 }
  157.                 s++;
  158.             }
  159.             killsp();
  160.             typ=INT;*buff=0;
  161.         }
  162.         if(!strcmp("void",buff)) {typ=settyp(VOID,typ);notdone=1;}
  163.         if(!strcmp("char",buff)) {typ=settyp(CHAR,typ);notdone=1;}
  164.         if(!strcmp("short",buff)) {typ=settyp(SHORT,typ);notdone=1;}
  165.         if(!strcmp("int",buff)) {typ=settyp(INT,typ);notdone=1;}
  166.         if(!strcmp("long",buff)) {typ=settyp(LONG,typ);notdone=1;}
  167.         if(!strcmp("float",buff)) {typ=settyp(FLOAT,typ);notdone=1;}
  168.         if(!strcmp("double",buff)) {typ=settyp(DOUBLE,typ);notdone=1;}
  169.         if(!strcmp("const",buff)) {type_qualifiers|=CONST;notdone=1;}
  170.         if(!strcmp("volatile",buff)) {type_qualifiers|=VOLATILE;notdone=1;}
  171.         if(!strcmp("unsigned",buff)) { notdone=1;type_qualifiers|=UNSIGNED;}
  172.         if(!strcmp("signed",buff)) notdone=1; /* hier ueberall strengere Systnaxpruefung */
  173.         if(!strcmp("auto",buff)) {dsc;storage_class=AUTO;notdone=1;}
  174.         if(!strcmp("register",buff)){dsc;storage_class=REGISTER;notdone=1;}
  175.         if(!strcmp("static",buff)) {dsc;storage_class=STATIC;notdone=1;}
  176.         if(!strcmp("extern",buff)) {dsc;storage_class=EXTERN;notdone=1;}
  177.         if(!strcmp("typedef",buff)) {dsc;storage_class=TYPEDEF;notdone=1;}
  178.  
  179.         if(!notdone&&*buff){
  180.             v=find_var(buff,0);
  181.             if(v&&v->storage_class==TYPEDEF){
  182.                 struct struct_declaration *sd;
  183.                 free(new);
  184.                 new=clone_typ(v->vtyp);
  185.                 if((new->flags&15)!=ARRAY&&(new->flags&UNCOMPLETE))
  186.                     if(sd=find_struct((void *)new->exact)){new->flags&=~UNCOMPLETE;new->exact=sd;}
  187.                 typ=new->flags;
  188.                 notdone=1;
  189.             }
  190.         }
  191.         if(DEBUG&2) printf("typ:%d\n",typ);
  192.         killsp();
  193.     }while(notdone);
  194.     s=merk;
  195.     return_sc=storage_class;
  196.     if(typ==0){
  197.         if(storage_class==0&&type_qualifiers==0) {free(new);return(0);}
  198.         typ=INT;
  199.     }
  200.     if(type_qualifiers&1){if(typ>LONG) error(58);}
  201.     if(DEBUG&2) printf("ts finish:%s\n",s);
  202.     new->flags=typ|type_qualifiers;
  203.     return(new);
  204. }
  205.  
  206.  
  207. struct Typ *declarator(struct Typ *a)
  208. /* Erzeugt einen neuen Typ, auf Basis des Typs a            */
  209. /* a wird hiermit verkettet                                 */
  210. {
  211.     struct Typ *t;
  212.     killsp();*ident=0;
  213.     t=direct_declarator(pointer(a));
  214.     if(!a) {if(t) freetyp(t);return(0);} else return(t);
  215. }
  216. struct Typ *pointer(struct Typ *a)
  217. /* Unterroutine fuer declaration(), behandelt Zeiger auf Typ    */
  218. /* Kann sein, dass die Assoziativitaet nicht stimmt */
  219. {
  220.     struct Typ *t;char *merk;int notdone;
  221.     if(!a) return(0);
  222.     killsp();
  223.     while(*s=='*'){
  224.         s++;
  225.         t=(struct Typ *)mymalloc(TYPS);
  226.         if(MDEBUG) printf("malloc %p\n",t);
  227.         t->flags=POINTER;
  228.         t->next=a;
  229.         a=t;
  230.         do{
  231.             killsp();
  232.             merk=s;cpbez(buff);
  233.             notdone=0;
  234.             if(!strcmp("const",buff)) {a->flags|=CONST;notdone=1;}
  235.             if(!strcmp("volatile",buff)) {a->flags|=VOLATILE;notdone=1;}
  236.         }while(notdone);
  237.         s=merk;
  238.     }
  239.     return(a);
  240. }
  241.  
  242. struct Typ *direct_declarator(struct Typ *a)
  243. /*  Unterroutine zu declarator()                    */
  244. /* behandelt [],(funkt),(dekl)                      */
  245. /* Funktionesufrufe und Arrays noch unvollstaendig  */
  246. /* implementiert, auch Assoziativitaet zweifelhaft  */
  247. {
  248.     struct Typ *rek=0,*merk,*p,*t,*first,*last=0;
  249.     struct struct_declaration *fsd;
  250.     char *imerk,fbuff[MAXI];
  251.     killsp();
  252.     if(!isalpha(*s)&&*s!='_'&&*s!='('&&*s!='[') return(a);
  253.     if(isalpha(*s)||*s=='_'){
  254.         cpbez(ident);
  255.         if(!a) return(0);
  256.     }else if(*s=='('&&a){
  257.         /* Rekursion */
  258.         imerk=s; s++; killsp();
  259.         if(*s!=')'&&*ident==0&&!declaration(0)){
  260.             merk=a;
  261.             rek=declarator(a);
  262.             if(*s!=')') error(59); else s++;
  263.         }else s=imerk;
  264.     }
  265.     if(!a)return(0);
  266.     killsp();
  267.     while(*s=='['||*s=='('){
  268.         if(*s=='['){
  269.             s++;
  270.             killsp();
  271.             p=(struct Typ *)mymalloc(TYPS);
  272.             if(MDEBUG) printf("malloc %p\n",p);
  273.             p->flags=ARRAY;
  274.             p->next=0;
  275.             if(*s==']'){
  276.                 p->size=0;
  277.                 p->flags|=UNCOMPLETE;
  278.             }else{
  279.                 np tree;unsigned long l;
  280.                 tree=expression();
  281.                 if(!type_expression(tree)){
  282. /*                    error("incorrect constant expression");*/
  283.                 }else{
  284.                     if(tree->sidefx) error(60);
  285.                     if(tree->flags!=CEXPR||(tree->ntyp->flags&15)<CHAR||(tree->ntyp->flags&15)>LONG){
  286.                         error(19);
  287.                     }else{
  288.                         eval_constn(tree);
  289.                         l=zul2ul(vulong);
  290.                         p->size=l;
  291.                         if(p->size==0) {error(61);p->size=1;}
  292.                     }
  293.                 }
  294.                 free_expression(tree);
  295.             }
  296.             if(*s!=']') error(62); else s++;
  297.             if(last){
  298.                 last->next=p;
  299.                 last=p;
  300.             }else{
  301.                 first=last=p;
  302.             }
  303.         }
  304.         if(*s=='('){
  305.             int komma;
  306.             /* Identifier- oder Parameter-list noch nicht komplett */
  307.             /* z.B. ... oder ohne Parameter                        */
  308.             s++;
  309.             killsp();
  310.             fsd=(struct struct_declaration *)mymalloc(MAXSTRUCTSIZE);
  311.             fsd->count=0;
  312.             imerk=ident;komma=0;
  313.             while(*s!=')'&&*s!='.'){
  314.                 ident=fbuff;*fbuff=0;komma=0;
  315.                 t=declarator(declaration_specifiers());
  316.                 if(!t&&*ident==0) {error(20);
  317.                                    break;}
  318.                 if(fsd->count){
  319.                     if((t&&!fsd->sl[fsd->count-1].styp)||
  320.                        (!t&&fsd->sl[fsd->count-1].styp))
  321.                         error(63);
  322.                 }
  323.                 if(!return_sc) return_sc=AUTO;
  324.                 if(return_sc!=AUTO&&return_sc!=REGISTER)
  325.                     {error(21);return_sc=AUTO;}
  326.                 fsd->sl[fsd->count].styp=t;
  327.                 fsd->sl[fsd->count].storage_class=return_sc;
  328.                 fsd->sl[fsd->count].identifier=add_identifier(ident,strlen(ident));
  329.                 if(t){
  330.                     if((fsd->sl[fsd->count].styp->flags&15)==VOID&&fsd->count!=0)
  331.                         error(22);
  332.                     /*  Arrays in Zeiger umwandeln  */
  333.                     if((fsd->sl[fsd->count].styp->flags&15)==ARRAY)
  334.                         fsd->sl[fsd->count].styp->flags=POINTER;
  335.                     /*  Funktionen in Zeiger auf Funktionen umwandeln   */
  336.                     if((fsd->sl[fsd->count].styp->flags&15)==FUNKT){
  337.                         struct Typ *new;
  338.                         new=(struct Typ *)mymalloc(TYPS);
  339.                         new->flags=POINTER;
  340.                         new->next=fsd->sl[fsd->count].styp;
  341.                         fsd->sl[fsd->count].styp=new;
  342.                     }
  343.  
  344.                 }
  345.                 fsd->count++;
  346.                 killsp(); /* Hier Syntaxpruefung strenger machen */
  347.                 if(*s==',') {s++;komma=1; killsp();}
  348.             }
  349.             ident=imerk;
  350.             if((*s!='.'||*(s+1)!='.'||*(s+2)!='.')||!komma){
  351.                 if(fsd->count>0&&(!fsd->sl[fsd->count-1].styp||(fsd->sl[fsd->count-1].styp->flags&15)!=VOID)){
  352.                     fsd->sl[fsd->count].styp=(struct Typ *)mymalloc(TYPS);
  353.                     fsd->sl[fsd->count].styp->flags=VOID;
  354.                     fsd->sl[fsd->count].styp->next=0;
  355.                     fsd->sl[fsd->count].identifier=empty;
  356.                     fsd->count++;
  357.                 }
  358.             }else if(komma) {s+=3;komma=0;}
  359.             p=(struct Typ *)mymalloc(TYPS);
  360.             if(MDEBUG) printf("malloc %p\n",p);
  361.             p->flags=FUNKT;
  362.             p->next=0;
  363.             p->exact=add_sd(fsd);
  364.             free(fsd);
  365.             killsp();
  366.             if(*s!=')'||komma) error(59); else s++;
  367.             if(last){
  368.                 last->next=p;
  369.                 last=p;
  370.             }else{
  371.                 first=last=p;
  372.             }
  373.         }
  374.         killsp();
  375.     }
  376.     if(last){last->next=a;last=a;a=first;}
  377.     if(rek!=0&&rek!=merk){
  378.         /* Zweite Liste anhaengen */
  379.         p=rek;
  380.         while(p->next!=merk) p=p->next;
  381.         if(p) p->next=a; else ierror(0);
  382.         return(rek);
  383.     }
  384.     return(a);
  385. }
  386. int declaration(int offset)
  387. /* Testet, ob nach offset Teichen eine Typangabe kommt  */
  388. /* Hier muss noch Test auf typedefs eingebaut werden */
  389. {
  390.     char *merk=s,buff[MAXI];
  391.     struct Var *v;
  392.     s+=offset;
  393.     killsp();
  394.     /*printf("d:%s\n",s);*/
  395.  
  396.     cpbez(buff);
  397.     s=merk;
  398.     if(!strcmp("auto",buff)) return(1);
  399.     if(!strcmp("char",buff)) return(1);
  400.     if(!strcmp("const",buff)) return(1);
  401.     if(!strcmp("double",buff)) return(1);
  402.     if(!strcmp("enum",buff)) return(1);
  403.     if(!strcmp("extern",buff)) return(1);
  404.     if(!strcmp("float",buff)) return(1);
  405.     if(!strcmp("int",buff)) return(1);
  406.     if(!strcmp("long",buff)) return(1);
  407.     if(!strcmp("register",buff)) return(1);
  408.     if(!strcmp("short",buff)) return(1);
  409.     if(!strcmp("signed",buff)) return(1);
  410.     if(!strcmp("static",buff)) return(1);
  411.     if(!strcmp("struct",buff)) return(1);
  412.     if(!strcmp("typedef",buff)) return(1);
  413.     if(!strcmp("union",buff)) return(1);
  414.     if(!strcmp("unsigned",buff)) return(1);
  415.     if(!strcmp("void",buff)) return(1);
  416.     if(!strcmp("volatile",buff)) return(1);
  417.     v=find_var(buff,0);
  418.     if(v&&v->storage_class==TYPEDEF) return(1);
  419.     return(0);
  420. }
  421.  
  422. struct struct_declaration *add_sd(struct struct_declaration *decl)
  423. /*  Erzeugt eine Kopie einer struct_declaration und fuegt sie   */
  424. /* in die passende Liste ein; Original kann dann geloescht      */
  425. /* werden.                                                      */
  426. {
  427.     int size;struct struct_declaration *new;
  428.     size=((decl->count)-1)*sizeof(struct struct_list)+sizeof(struct struct_declaration);
  429.     new=(struct struct_declaration *)mymalloc(size);
  430.     memcpy(new,decl,size);
  431.     new->next=0;
  432.     if(first_sd[nesting]==0){
  433.         first_sd[nesting]=last_sd[nesting]=new;
  434.     }else{
  435.         last_sd[nesting]->next=new;
  436.         last_sd[nesting]=new;
  437.     }
  438.     return(last_sd[nesting]); /* return(new) sollte aequiv. sein */
  439. }
  440. void free_sd(struct struct_declaration *p)
  441. /*  Gibt eine struct_declaration-List inkl. struct_lists und    */
  442. /*  allen Typen jeder struct_list frei, nicht aber identifier   */
  443. {
  444.     int i;struct struct_declaration *merk;
  445.     while(p){
  446.         merk=p->next;
  447.         for(i=0;i<p->count;i++) if(p->sl[i].styp) freetyp(p->sl[i].styp);
  448.         free(p);
  449.         p=merk;
  450.     }
  451. }
  452. char *add_identifier(char *identifier,int length)
  453. /*  Kopiert identifier an sicheren Ort, der spaeter zentral     */
  454. /*  freigegeben werden kann.                                    */
  455. /*  Sollte noch einbauen, dass ueberprueft wird, ob schon       */
  456. /*  vorhanden und dann nicht zweimal speichern                  */
  457. {
  458.     struct identifier_list *new;
  459.     if((*identifier==0&&length==0)||identifier==empty) return(empty);
  460.     new=(struct identifier_list*)mymalloc(sizeof(struct identifier_list));
  461.     new->identifier=(char *)mymalloc(length+1);
  462.     memcpy(new->identifier,identifier,length+1);
  463.     new->next=0;new->length=length;
  464.     if(last_ilist[nesting]){
  465.         last_ilist[nesting]->next=new;
  466.         last_ilist[nesting]=new;
  467.     }else{
  468.         last_ilist[nesting]=first_ilist[nesting]=new;
  469.     }
  470.     return(new->identifier);
  471. }
  472. void free_ilist(struct identifier_list *p)
  473. /*  Gibt eine verkettete identifier_liste und saemtliche darin  */
  474. /*  gespeicherten Identifier frei.                              */
  475. {
  476.     struct identifier_list *merk;
  477.     while(p){
  478.         merk=p->next;
  479.         if(p->identifier) free(p->identifier);
  480.         free(p);
  481.         p=merk;
  482.     }
  483. }
  484. int type_uncomplete(struct Typ *p)
  485. /*  Testet, ob Typ unvollstaendig ist. Momentan gelten nur      */
  486. /*  unvollstaendige Strukturen und Arrays von solchen als       */
  487. /*  unvollstaendig, aber keine Zeiger oder Funktionen darauf    */
  488. {
  489.     struct struct_declaration *sd;
  490.     if(!p){ierror(0);return(0);}
  491.     if((p->flags&15)!=ARRAY&&(p->flags&UNCOMPLETE)){
  492.         if(sd=find_struct((void *)p->exact)){p->flags&=~UNCOMPLETE;p->exact=sd;}
  493.     }
  494.     if(p->flags&UNCOMPLETE) return(1);
  495.     if(((p->flags&15)==ARRAY)&&type_uncomplete(p->next)) return(1);
  496.     return(0);
  497. }
  498. void add_struct_identifier(char *identifier,struct struct_declaration *sd)
  499. /*  Erzeugt neuen struct_identifier, fuegt ihn in Liste an und  */
  500. /*  vervollstaendigt unvollstaendige Typen dieser Struktur.     */
  501. {
  502.     struct struct_identifier *new;
  503. /*    struct Typ *t;*/
  504.     if(DEBUG&2) printf("add_si %s\n",identifier);
  505.     new=(struct struct_identifier *)mymalloc(sizeof(struct struct_identifier));
  506.     new->identifier=add_identifier(identifier,strlen(identifier));
  507.     new->sd=sd; new->next=0;
  508.     if(first_si[nesting]==0){
  509.         first_si[nesting]=new;last_si[nesting]=new;
  510.     }else{
  511.         last_si[nesting]->next=new;last_si[nesting]=new;
  512.     }
  513. /*    while(t=find_uncomplete_struct(identifier)){
  514.         printf("Ersetze uncomplete %u\n",t);
  515.         t->flags&=~UNCOMPLETE;t->exact=sd;
  516.     }*/
  517. }
  518. void free_si(struct struct_identifier *p)
  519. /*  Gibt eine struct_identifier-Liste frei, aber nicht die      */
  520. /*  identifiers und struct_declarations                         */
  521. {
  522.     struct struct_identifier *merk;
  523.     while(p){
  524.         merk=p->next;
  525.         free(p);
  526.         p=merk;
  527.     }
  528. }
  529. struct struct_declaration *find_struct(char *identifier)
  530. /*  Sucht angegebene Strukturdefinition und liefert             */
  531. /*  entsprechende struct_declaration                            */
  532. {
  533.     struct struct_identifier *si; int i;
  534.     for(i=nesting;i>=0;i--){
  535.         si=first_si[i];
  536.         while(si){
  537.             if(!strcmp(si->identifier,identifier)) return(si->sd);
  538.             si=si->next;
  539.         }
  540.     }
  541.     return(0);
  542. }
  543. struct Var *add_var(char *identifier, struct Typ *t, int storage_class,struct const_list *clist)
  544. /*  Fuegt eine Variable mit Typ in die var_list ein             */
  545. /*  maschinenspezifisches und Codegeneration fehlen noch        */
  546. /*  Alignment maschinenabhaengig                                */
  547. /*  In der storage_class werden die Flags PARAMETER und evtl.   */
  548. /*  OLDSTYLE erkannt.                                           */
  549. {
  550.     struct Var *new;int f;
  551.     struct struct_declaration *sd;
  552.     /*if(*identifier==0) return;*/ /* sollte woanders bemaekelt werden */
  553.     if(DEBUG&2) printf("add_var(): %s\n",identifier);
  554.     if((t->flags&15)==FUNKT&&((t->next->flags&15)==ARRAY||(t->next->flags&15)==FUNKT))
  555.         error(25);
  556.     if((t->flags&15)!=ARRAY&&(t->flags&UNCOMPLETE))
  557.         if(sd=find_struct((void *)t->exact)){t->flags&=~UNCOMPLETE;t->exact=sd;}
  558.     new=(struct Var *)mymalloc(sizeof(struct Var));
  559.     new->identifier=add_identifier(identifier,strlen(identifier));
  560.     new->clist=clist;
  561.     new->vtyp=t;
  562.     new->storage_class=storage_class&7;
  563.     new->next=0;
  564.     new->flags=0;
  565.     new->nesting=nesting;
  566. /*    if((storage_class&7)==STATIC||(storage_class&7)==EXTERN) new->flags=USEDASSOURCE|USEDASDEST;*/
  567.     if(storage_class&PARAMETER) new->flags=USEDASDEST;
  568.     if((storage_class&7)==REGISTER) new->priority=registerpri; else new->priority=0;
  569.     if(last_var[nesting]){
  570.         new->offset=last_var[nesting]->offset+szof(last_var[nesting]->vtyp);
  571.         last_var[nesting]->next=new;
  572.         last_var[nesting]=new;
  573.     }else{
  574.         new->offset=0;
  575.         first_var[nesting]=last_var[nesting]=new;
  576.     }
  577.     f=t->flags&15;
  578.     if((storage_class&7)==AUTO||(storage_class&7)==REGISTER){
  579.         new->offset=local_offset[nesting];
  580.         new->offset=((new->offset+align[f]-1)/align[f])*align[f];
  581.         if((storage_class&PARAMETER)&&f>=CHAR&&f<=SHORT){
  582.         /*  Integer-Erweiterungen fuer alle Funktionsparameter  */
  583.             local_offset[nesting]=new->offset+sizetab[INT];
  584.         }else{
  585.             local_offset[nesting]=new->offset+szof(new->vtyp);
  586.         }
  587.         /*  Bei alten Funktionen werden FLOAT als DOUBLE uebergeben */
  588.         if((storage_class&(PARAMETER|OLDSTYLE))==(PARAMETER|OLDSTYLE)&&f==FLOAT)
  589.             local_offset[nesting]+=sizetab[DOUBLE]-sizetab[FLOAT];
  590.  
  591.         if(local_offset[nesting]>max_offset) max_offset=local_offset[nesting];
  592.     }
  593.     if((storage_class&7)==STATIC) new->offset=++label;
  594.     if(storage_class&PARAMETER){
  595. /*        new->storage_class&=~PARAMETER;*/
  596.         /* ob das hier so funktioniert ? Bei BIGENDIAN nimmt man den    */
  597.         /* hinteren Teil des INTs, bei LOWENDIAN muesste man eigentlich */
  598.         /* nichts tun, oder? Datenformate, bei denen wirklich           */
  599.         /* umgewandelt werden muss, werden (noch?) nicht unterstuetzt.  */
  600.         /* Ob sowas aber ueberhaupt zulaessig waere, weiss ich nicht.   */
  601.         if(f>=CHAR&&f<=SHORT&&sizetab[f]<sizetab[INT]){
  602.             if(BIGENDIAN)
  603.                 new->offset+=sizetab[INT]-sizetab[f];
  604.         }
  605.         if((storage_class&OLDSTYLE)&&f==FLOAT){
  606.         /*  Bei alten Funktionen werden DOUBLE nach FLOAT konvertiert   */
  607.             struct IC *conv=(struct IC *)mymalloc(ICS);
  608.             conv->code=CONVDOUBLE;
  609.             conv->typf=FLOAT;
  610.             conv->q1.flags=VAR|DONTREGISTERIZE;
  611.             conv->z.flags=VAR;
  612.             conv->q2.flags=0;
  613.             conv->q1.v=conv->z.v=new;
  614.             conv->q1.val.vlong=conv->z.val.vlong=l2zl(0);
  615.             add_IC(conv);
  616.             new->flags|=CONVPARAMETER;
  617.         }
  618.         new->offset=-new->offset;
  619.     }
  620.     return(new);
  621. }
  622. void free_var(struct Var *p)
  623. /*  Gibt Variablenliste inkl. Typ, aber ohne Identifier frei    */
  624. {
  625.     struct Var *merk;
  626.     while(p){
  627.         merk=p->next;
  628.         if(!(p->flags&USEDASADR)&&(p->storage_class==AUTO||p->storage_class==REGISTER)){
  629.             if(!(p->flags&USEDASDEST)&&(p->vtyp->flags&15)<=POINTER) error(64,p->identifier);
  630.             if(!(p->flags&USEDASSOURCE)&&(p->vtyp->flags&15)<=POINTER) error(65,p->identifier);
  631.         }
  632.         if(DEBUG&2) printf("free_var %s, pri=%d\n",p->identifier,p->priority);
  633.         if(p->vtyp) freetyp(p->vtyp);
  634.         if(p->clist) free_clist(p->clist);
  635.         free(p);
  636.         p=merk;
  637.     }
  638. }
  639. struct Var *find_var(char *identifier,int endnesting)
  640. /*  sucht Variable mit Bezeichner und liefert Zeiger zurueck    */
  641. /*  es werden nur Variablen der Bloecke endnesting-nesting      */
  642. /*  durchsucht                                                  */
  643. {
  644.     int i;struct Var *v;
  645.     if(*identifier==0||identifier==0) return(0);
  646.     for(i=nesting;i>=endnesting;i--){
  647.         v=first_var[i];
  648.         while(v){
  649.             if(!strcmp(v->identifier,identifier)) return(v);
  650.             v=v->next;
  651.         }
  652.     }
  653.     return(0);
  654. }
  655. void var_declaration(void)
  656. /*  Bearbeitet eine Variablendeklaration und erzeugt alle       */
  657. /*  noetigen Strukturen                                         */
  658. {
  659.     struct Typ *ts,*t,*old=0,*om=0;char *imerk,vident[MAXI];int mdef=0,makeint=0;
  660.     struct Var *v;int notdone,storage_class,msc;int extern_flag,isfunc;
  661.     ts=declaration_specifiers();notdone=1;
  662.     storage_class=return_sc;
  663.     if(storage_class==EXTERN) extern_flag=1; else extern_flag=0;
  664.     killsp();
  665.     if(*s==';'){
  666.         if(storage_class||((ts->flags&15)!=STRUCT&&(ts->flags&15)!=UNION&&(ts->flags&15)!=INT))
  667.             error(36);
  668.         freetyp(ts);s++;killsp();
  669.         return;
  670.     }
  671.     if(nesting==0&&(storage_class==AUTO||storage_class==REGISTER))
  672.         {error(66);storage_class=EXTERN;}
  673.     if(!ts){
  674.         if(nesting==0){
  675.             ts=(struct Typ *)mymalloc(TYPS);
  676.             ts->flags=INT;ts->next=0;
  677.             makeint=1;
  678.             if(!storage_class) storage_class=EXTERN;
  679.             error(67);
  680.         }else{
  681.             ierror(0);return;
  682.         }
  683.     }
  684.     if(storage_class==0){
  685.         if(nesting==0) storage_class=EXTERN; else storage_class=AUTO;
  686.     }
  687.     msc=storage_class;
  688.     while(notdone){
  689.         imerk=ident;ident=vident;*vident=0;  /* merken von ident hier vermutlich */
  690.         storage_class=msc;
  691.         if(old) {freetyp(old);old=0;}
  692.         t=declarator(clone_typ(ts));
  693.         if((t->flags&15)!=FUNKT) isfunc=0;
  694.             else {isfunc=1;if(storage_class!=STATIC) storage_class=EXTERN;}
  695.         ident=imerk;                    /* nicht unbedingt noetig ?         */
  696.         v=find_var(vident,nesting);
  697.         if(v){
  698.             if(nesting>0&&(v->flags&DEFINED)&&!extern_flag&&!isfunc){
  699.                 error(27,vident);
  700.             }else{
  701.                 if(t&&v->vtyp&&!compare_pointers(v->vtyp,t,255)){
  702.                     error(68,vident);
  703.                 }
  704.                 if(storage_class!=v->storage_class&&!extern_flag)
  705.                     error(28,v->identifier);
  706.                 if(!isfunc&&!extern_flag) v->flags|=TENTATIVE;
  707.             }
  708.             if(!isfunc){
  709.                 v->vtyp=t;
  710.             }else{
  711.                 om=v->vtyp;
  712.                 if(t->exact->count>0) {old=v->vtyp;v->vtyp=t;}
  713.             }
  714.         }else{
  715.             v=add_var(vident,t,storage_class,0);
  716.             if(!v) ierror(0);
  717.             else{
  718.                 if(!isfunc&&!extern_flag){
  719.                     v->flags|=TENTATIVE;
  720.                     if(nesting>0) v->flags|=DEFINED;
  721.                 }
  722.             }
  723.             om=0;
  724.         }
  725.         killsp();
  726.         /*  Initialisierung von Variablen bei Deklaration   */
  727.         if(*s=='='){
  728.             s++;killsp();
  729.             if(v->flags&DEFINED) {if(nesting==0) error(30,v->identifier);}
  730.                 else v->flags|=DEFINED;
  731.             if(v->storage_class==TYPEDEF) error(114,v->identifier);
  732.             if(extern_flag){
  733.                 error(118,v->identifier);
  734.                 if(v->storage_class!=EXTERN){ error(77);v->storage_class=EXTERN;}
  735.             }
  736.             v->clist=initialization(v->vtyp,v->storage_class==AUTO||v->storage_class==REGISTER);
  737.             if(v->clist){
  738.                 if((v->vtyp->flags&15)==ARRAY&&v->vtyp->size==0){
  739.                     struct const_list *p=v->clist;
  740.                     if(!(v->vtyp->flags&UNCOMPLETE)) ierror(0);
  741.                      else v->vtyp->flags&=~UNCOMPLETE;
  742.                     while(p){v->vtyp->size++;p=p->next;}
  743.                     local_offset[nesting]+=szof(v->vtyp);
  744.                     if(local_offset[nesting]>max_offset) max_offset=local_offset[nesting];
  745.                 }
  746.                 if(v->storage_class==AUTO||v->storage_class==REGISTER){
  747.                     struct IC *new;
  748.                 /*  Initialisierung von auto-Variablen  */
  749.                     new=(struct IC *)mymalloc(ICS);
  750.                     new->code=ASSIGN;
  751.                     new->typf=v->vtyp->flags;
  752.                     new->q2.flags=0;
  753.                     new->q2.reg=szof(v->vtyp);
  754.                     new->z.flags=VAR;
  755.                     new->z.v=v;
  756.                     new->z.val.vlong=l2zl(0L);
  757.                     if(v->clist->tree){
  758.                     /*  einzelner Ausdruck  */
  759.                         gen_IC(v->clist->tree,0,0);
  760.                         convert(v->clist->tree,v->vtyp->flags&31);
  761.                         new->q1=v->clist->tree->o;
  762. /*                        v->clist=0;*/
  763.                     }else{
  764.                     /*  Array etc.  */
  765.                         struct Var *nv;
  766.                         nv=add_var(empty,clone_typ(v->vtyp),STATIC,v->clist);
  767.                         nv->flags|=DEFINED;
  768. /*                        v->clist=0;*/
  769.                         new->q1.flags=VAR;
  770.                         new->q1.v=nv;
  771.                         new->q1.val.vlong=l2zl(0L);
  772.                     }
  773.                     add_IC(new);
  774. /*                    if(v->clist&&v->clist->tree){free_expression(v->clist->tree);v->clist->tree=0;}*/
  775.                 }
  776.             }
  777.         }else{
  778.             if((v->vtyp->flags&CONST)&&v->storage_class!=TYPEDEF&&!extern_flag)
  779.                 error(119,v->identifier);
  780.         }
  781.         if(*s==',') {s++;killsp();mdef=1;} else notdone=0;
  782.     }
  783.     freetyp(ts);
  784.     if(!mdef&&t&&(t->flags&15)==FUNKT&&*s!=';'){
  785.     /*  Funktionsdefinition                                     */
  786.         int i,oldstyle=0;
  787.         if(DEBUG&1) printf("Funktionsdefinition!\n");
  788.         if(nesting) error(32);
  789.         if(v->flags&DEFINED) error(33,v->identifier);
  790.             else v->flags|=DEFINED;
  791.         if(storage_class!=EXTERN&&storage_class!=STATIC) error(34);
  792.         if(extern_flag) error(120);
  793.         if(!strcmp(v->identifier,"main")&&(!t->next||t->next->flags!=INT)) error(121);
  794.         while(*s!='{'){
  795.         /*  alter Stil  */
  796.             struct Typ *nt=declaration_specifiers();notdone=1;oldstyle=OLDSTYLE;
  797.             if(!ts) {error(35);}
  798.             while(notdone){
  799.                 int found=0;
  800.                 imerk=ident;ident=vident;*vident=0;
  801.                 ts=declarator(clone_typ(nt));
  802.                 ident=imerk;
  803.                 if(!ts) {error(36);}
  804.                 else{
  805.                     for(i=0;i<t->exact->count;i++){
  806.                         if(!strcmp(t->exact->sl[i].identifier,vident)){
  807.                             found=1;
  808.                             if(t->exact->sl[i].styp){
  809.                                 error(69,vident);
  810.                                 freetyp(t->exact->sl[i].styp);
  811.                             }
  812.                             /*  typ[] in *typ   */
  813.                             if((ts->flags&15)==ARRAY) ts->flags=POINTER;
  814.                             /*  typ() in *typ() */
  815.                             if((ts->flags&15)==FUNKT){
  816.                                 struct Typ *new=(struct Typ *)mymalloc(TYPS);
  817.                                 new->flags=POINTER;
  818.                                 new->next=ts;
  819.                                 ts=new;
  820.                             }
  821.                             if(!return_sc) return_sc=AUTO;
  822.                             if(return_sc!=AUTO&&return_sc!=REGISTER)
  823.                                 {error(122);return_sc=AUTO;}
  824.                             t->exact->sl[i].storage_class=return_sc;
  825.                             t->exact->sl[i].styp=ts;
  826.                         }
  827.                     }
  828.                 }
  829.                 if(!found) {error(37,vident);}
  830.                 killsp();
  831.                 if(*s==',') {s++;killsp();} else notdone=0;
  832.             }
  833.             if(nt) freetyp(nt);
  834.             if(*s==';'){s++;killsp();
  835.             }else{
  836.                 error(54);
  837.                 while(*s!='{'&&*s!=';'){s++;killsp();}
  838.             }
  839.         }
  840.         if(t->exact->count==0){
  841.             /*  das hier setzt noch voraus, das das Array in    */
  842.             /*  struct_declaration 1 Element hat                */
  843.             struct struct_declaration sd;
  844.             if(DEBUG&1) printf("prototype converted to (void)\n");
  845.             sd.count=1;
  846.             sd.sl[0].identifier=empty;
  847.             sd.sl[0].storage_class=AUTO;
  848.             sd.sl[0].styp=mymalloc(TYPS);
  849.             sd.sl[0].styp->flags=VOID;
  850.             sd.sl[0].styp->next=0;
  851.             t->exact=add_sd(&sd);
  852.         }
  853.         if(om&&!compare_sd(om->exact,t->exact))
  854.             error(123);
  855.         nocode=0;currentpri=1;
  856.         enter_block();local_offset[1]=4;
  857.         first_ic=last_ic=0;
  858.         for(i=0;i<t->exact->count;i++){
  859.             if(!t->exact->sl[i].styp&&*t->exact->sl[i].identifier){
  860.                 struct Typ *nt;
  861.                 nt=(struct Typ *)mymalloc(TYPS);
  862.                 nt->flags=INT; nt->next=0;
  863.                 t->exact->sl[i].styp=nt;
  864.                 t->exact->sl[i].storage_class=AUTO;
  865.                 error(124);
  866.             }
  867.             if(*t->exact->sl[i].identifier){
  868.                 add_var(t->exact->sl[i].identifier,clone_typ(t->exact->sl[i].styp),t->exact->sl[i].storage_class|PARAMETER|oldstyle,0);
  869.                 if(oldstyle){
  870.                     freetyp(t->exact->sl[i].styp);
  871.                     t->exact->sl[i].styp=0; /*  Prototype entfernen */
  872.                 }
  873.             }
  874.         }
  875.         if(oldstyle) t->exact->count=0; /*  Prototype entfernen */
  876.         local_offset[1]=0;return_var=0;
  877.         if(!v->vtyp) ierror(0);
  878.         if(v->vtyp->next->flags==VOID) return_typ=0;
  879.         else{
  880.             return_typ=v->vtyp->next;
  881.             if(!freturn(return_typ)){
  882.                 return_var=add_var(empty,clone_typ(return_typ),STATIC,0);
  883.                 return_var->flags|=DEFINED;
  884.             }
  885.         }
  886.         return_label=++label;
  887.         v->flags|=GENERATED;
  888.         {int i;
  889.             for(i=1;i<=MAXR;i++) regs[i]=regused[i]=regsa[i];
  890.         }
  891.         max_offset=0;function_calls=0;float_used=0;
  892.         compound_statement();
  893.         {int i;
  894.             for(i=1;i<=MAXR;i++) if(regs[i]!=regsa[i]) {printf("Register %s:\n",regnames[i]);ierror(0);}
  895.         }
  896.         gen_label(return_label);
  897.         if(return_var){
  898.         /*  Zeiger in Returnregister schreiben, bei Funktionen, deren   */
  899.         /*  Rueckgabewert nicht in Registern uebergeben wird            */
  900.             int rreg;
  901.             struct Typ *pointer=(struct Typ *)mymalloc(TYPS);
  902.             struct IC *new=(struct IC *)mymalloc(ICS);
  903.             pointer->flags=POINTER;pointer->next=0;
  904.             rreg=freturn(pointer);
  905.             free(pointer);
  906.             if(regs[rreg]) ierror(0);
  907.             new->code=ALLOCREG;
  908.             new->q1.flags=REG;
  909.             new->q2.flags=new->z.flags=0;
  910.             new->typf=0;
  911.             new->q1.reg=rreg;
  912.             add_IC(new);
  913.             regs[rreg]=1;
  914.             new=(struct IC *)mymalloc(ICS);
  915.             new->code=ASSIGN;
  916.             new->typf=POINTER;
  917.             new->q1.flags=SCRATCH|VAR|VARADR;
  918.             new->q1.reg=0;
  919.             new->q1.v=return_var;
  920.             new->q1.val.vlong=l2zl(0L);
  921.             new->q2.flags=0;
  922.             new->q2.reg=sizetab[POINTER];
  923.             new->z.flags=REG;
  924.             new->z.reg=rreg;
  925.             add_IC(new);
  926.             free_reg(rreg);
  927.         }
  928.         if(first_ic){
  929.             if((c_flags[2]&USEDFLAG)&&ic1){fprintf(ic1,"function %s\n",v->identifier); pric(ic1,first_ic);}
  930.             if((c_flags[0]&USEDFLAG)&&(c_flags_val[0].l&1)) simple_regs();
  931.             if((c_flags[3]&USEDFLAG)&&ic2){fprintf(ic2,"function %s\n",v->identifier); pric(ic2,first_ic);}
  932.             if(out&&!(c_flags[5]&USEDFLAG)){
  933.                 gen_code(out,first_ic,v,max_offset);
  934.             }
  935.             free_IC(first_ic);first_ic=last_ic=0;
  936.         }
  937.         leave_block();
  938.     }else{
  939.         if(makeint) error(125);
  940.         if(*s==';') s++; else error(54);
  941.         if((t->flags&15)==FUNKT&&t->exact){
  942.             struct struct_declaration *sd=t->exact;int i,f;
  943.             for(f=0,i=0;i<sd->count;i++)
  944.                 if(!sd->sl[i].styp){error(126);f=1;}
  945.             if(f){
  946.                 for(i=0;i<sd->count;i++) if(sd->sl[i].styp) freetyp(sd->sl[i].styp);
  947.                 sd->count=0;
  948.             }
  949.         }
  950.     }
  951.     if(old) freetyp(old);
  952. }
  953. int storage_class_specifiers(void)
  954. /*  Gibt angegebene storage_class zurueck                   */
  955. /*  muss nach ANSI wohl in declaration_specifiers           */
  956. /*  integriert werden                                       */
  957. {
  958.     char *merk=s,buff[MAXI];
  959.     killsp();cpbez(buff);
  960.     if(!strcmp("auto",buff)) return(AUTO);
  961.     if(!strcmp("register",buff)) return(REGISTER);
  962.     if(!strcmp("static",buff)) return(STATIC);
  963.     if(!strcmp("extern",buff)) return(EXTERN);
  964.     if(!strcmp("typedef",buff)) return(TYPEDEF);
  965.     s=merk;return(0);
  966. }
  967. struct Typ *clone_typ(struct Typ *old)
  968. /*  Erzeugt Kopie eines Typs und liefert Zeiger auf Kopie   */
  969. {
  970.     struct Typ *new;
  971.     if(!old) return(0);
  972.     new=(struct Typ *)mymalloc(TYPS);
  973.     *new=*old;
  974.     if(new->next) new->next=clone_typ(new->next);
  975.     return(new);
  976. }
  977. int compare_pointers(struct Typ *a,struct Typ *b,int qual)
  978. /*  vergleicht, ob Typ beider Typen gleich ist, const/volatile      */
  979. /*  werden laut ANSI nicht beruecksichtigt                          */
  980. {
  981.     struct struct_declaration *sd;
  982.     int af=a->flags&qual,bf=b->flags&qual;
  983.     if(af!=bf) return(0);
  984.     af&=15;bf&=15;
  985.     if(af!=ARRAY&&(a->flags&UNCOMPLETE))
  986.         if(sd=find_struct((void *)a->exact)){a->flags&=~UNCOMPLETE;a->exact=sd;}
  987.     if(bf!=ARRAY&&(b->flags&UNCOMPLETE))
  988.         if(sd=find_struct((void *)b->exact)){b->flags&=~UNCOMPLETE;b->exact=sd;}
  989.     if(af==FUNKT){
  990.         if(a->exact->count&&!compare_sd(a->exact,b->exact)) return(0);
  991.     }
  992.     if(af==STRUCT||af==UNION){
  993.         if((a->flags&UNCOMPLETE)!=(b->flags&UNCOMPLETE)) return(0);
  994.         if(a->flags&UNCOMPLETE){
  995.             if(strcmp((char *)a->exact,(char *)b->exact)) return(0);
  996.         }else{
  997.             if(a->exact!=b->exact) return(0);
  998.         }
  999.     }
  1000.     if(af==ARRAY){
  1001.         if(a->size&&b->size&&a->size!=b->size) return(0);
  1002.     }
  1003.     if(a->next==0&&b->next!=0) return(0);
  1004.     if(a->next!=0&&b->next==0) return(0);
  1005.     if(a->next==0&&b->next==0) return(1);
  1006.     return(compare_pointers(a->next,b->next,qual));
  1007. }
  1008. int compare_sd(struct struct_declaration *a,struct struct_declaration *b)
  1009. /*  Vergleicht, ob zwei struct_declarations identisch sind          */
  1010. /*  Wird nur nur fuer Prototypen benutzt, leere Liste immer gleich  */
  1011. {
  1012.     int i;
  1013.     if(!a->count||!b->count) return(1);
  1014.     if(a->count!=b->count) return(0);
  1015.     for(i=0;i<a->count;i++)
  1016.         if(a->sl[i].styp&&b->sl[i].styp&&!compare_pointers(a->sl[i].styp,b->sl[i].styp,255)) return(0);
  1017.     return(1);
  1018. }
  1019. void free_clist(struct const_list *p)
  1020. /*  gibt clist frei                                         */
  1021. {
  1022.     struct const_list *merk;
  1023.     return;
  1024.     while(p){
  1025.         merk=p->next;
  1026.         if(p->other) free_clist(p->other);
  1027.         if(p->tree) free_expression(p->tree);
  1028.         free(p);
  1029.         p=merk;
  1030.     }
  1031. }
  1032. void gen_clist(FILE *,struct Typ *,struct const_list *);
  1033.  
  1034. void gen_vars(struct Var *v)
  1035. /*  generiert Variablen                                     */
  1036. {
  1037.     int mode,al;struct Var *p;
  1038.     for(mode=0;mode<3;mode++){
  1039.         for(al=maxalign;al>=1;al--){
  1040.             int i,flag;
  1041.             for(i=1,flag=0;i<15;i++) if(align[i]==al) flag=1;
  1042.             if(!flag) continue;
  1043.             gen_align(out,al);
  1044.             for(p=v;p;p=p->next){
  1045.                 if(DEBUG&2) printf("gen_var(): %s\n",p->identifier);
  1046.                 if(p->storage_class==STATIC||p->storage_class==EXTERN){
  1047.                     if(!(p->flags&GENERATED)){
  1048.                         if(p->storage_class==EXTERN&&!(p->flags&(USEDASSOURCE|USEDASDEST))&&!(p->flags&(TENTATIVE|DEFINED))) continue;
  1049.                         if((p->vtyp->flags&15)!=ARRAY){
  1050.                             if(align[p->vtyp->flags&15]!=al) continue;
  1051.                         }else{
  1052.                             if(align[p->vtyp->next->flags&15]!=al) continue;
  1053.                         }
  1054.                         if(mode==0&&(!p->clist||!(p->vtyp->flags&CONST))) continue;
  1055.                         if(mode==1&&!p->clist) continue;
  1056.                         if(mode==2&&p->clist) continue;
  1057.                         gen_var_head(out,p);
  1058.                         if(!(p->flags&(TENTATIVE|DEFINED))){
  1059.                             if(p->storage_class==STATIC) error(127,p->identifier);
  1060.                             continue;
  1061.                         }
  1062.                         if(!p->clist) gen_ds(out,szof(p->vtyp),p->vtyp);
  1063.                          else gen_clist(out,p->vtyp,p->clist);
  1064.                         p->flags|=GENERATED;
  1065.                     }
  1066.                 }
  1067.             }
  1068.         }
  1069.     }
  1070. }
  1071. void gen_clist(FILE *f,struct Typ *t,struct const_list *cl)
  1072. /*  generiert dc fuer const_list                            */
  1073. /*  hier ist noch einiges zu tun                            */
  1074. {
  1075.     int i;
  1076.     if((t->flags&15)==ARRAY){
  1077.         for(i=0;i<t->size&&cl;i++,cl=cl->next){
  1078.             if(!cl->other){ierror(0);return;}
  1079.             gen_clist(f,t->next,cl->other);
  1080.         }
  1081.         if(i<t->size) gen_ds(f,(t->size-i)*szof(t->next),t->next);
  1082.         return;
  1083.     }
  1084.     if((t->flags&15)==UNION){
  1085.         long rest;
  1086.         gen_clist(f,t->exact->sl[0].styp,cl);
  1087.         rest=szof(t)-szof(t->exact->sl[0].styp);
  1088.         if(rest) gen_ds(f,rest,0);
  1089.         return;
  1090.     }
  1091.     if((t->flags&15)==STRUCT){
  1092.         int size=0,al;struct Typ *st;
  1093.         for(i=0;i<t->exact->count&&cl;i++){
  1094.             if(!cl->other){ierror(0);return;}
  1095.             st=t->exact->sl[i].styp;
  1096.             al=align[st->flags&15];
  1097.             if(size%al) {gen_ds(f,al-size%al,0);size+=al-size%al;}
  1098.             if(!t->exact->sl[i].identifier) ierror(0);
  1099.             if(t->exact->sl[i].identifier[0]){
  1100.                 gen_clist(f,st,cl->other);
  1101.                 cl=cl->next;
  1102.             }else{
  1103.                 gen_ds(f,szof(st),0); /* sollte unnamed bitfield sein */
  1104.             }
  1105.             size+=szof(st);
  1106.         }
  1107.         for(;i<t->exact->count;i++){
  1108.             st=t->exact->sl[i].styp;
  1109.             al=align[st->flags&15];
  1110.             if(size%al) {gen_ds(f,al-size%al,0);size+=al-size%al;}
  1111.             gen_ds(f,szof(t->exact->sl[i].styp),t->exact->sl[i].styp);
  1112.             size+=szof(st);
  1113.         }
  1114.         al=align[STRUCT];
  1115.         if(size%al) gen_ds(f,al-size%al,0);
  1116.         return;
  1117.     }
  1118.     gen_dc(f,t->flags&31,cl);
  1119. }
  1120. struct const_list *initialization(struct Typ *t,int noconst)
  1121. /*  traegt eine Initialisierung in eine const_list ein          */
  1122. {
  1123.     struct const_list *first,*cl,**prev;np tree,tree2;int i,bracket;
  1124.     if((t->flags&15)==FUNKT){error(42);return(0);}
  1125.     if((t->flags&15)==ARRAY){
  1126.         if(*s=='\"'){
  1127.             killsp();
  1128.             tree=string_expression();
  1129.             first=(struct const_list *)tree->identifier;
  1130.             free_expression(tree);
  1131.             return(first);
  1132.         }
  1133.         if(*s=='{'){s++;killsp();bracket=1;} else bracket=0;
  1134.         prev=0;
  1135.         for(i=0;(!t->size||i<t->size)&&*s!='}';i++){
  1136.             cl=(struct const_list *)mymalloc(CLS);
  1137.             cl->next=0;cl->tree=0;
  1138.             cl->other=initialization(t->next,0);
  1139.             killsp();
  1140.             if(*s==','){s++;killsp();}
  1141.             if(prev) *prev=cl; else first=cl;
  1142.             prev=&cl->next;
  1143.         }
  1144.         if(bracket)
  1145.             if(*s=='}'){s++;killsp();} else error(128);
  1146.         return(first);
  1147.     }
  1148.     if((t->flags&15)==STRUCT&&(*s=='{'||!noconst)){
  1149.         if(t->flags&UNCOMPLETE)
  1150.             {error(43);return(0);}
  1151.         if(*s=='{'){s++;killsp();bracket=1;} else bracket=0;
  1152.         prev=0;
  1153.         for(i=0;i<t->exact->count&&*s!='}';i++){
  1154.             if(t->exact->sl[i].identifier[0]==0) {continue;} /* unnamed bitfield */
  1155.             cl=(struct const_list *)mymalloc(CLS);
  1156.             cl->next=0;cl->tree=0;
  1157.             cl->other=initialization(t->exact->sl[i].styp,0);
  1158.             if(*s==','){s++;killsp();}
  1159.             if(prev) *prev=cl; else first=cl;
  1160.             prev=&cl->next;
  1161.         }
  1162.         if(bracket)
  1163.             if(*s=='}'){s++;killsp();} else error(128);
  1164.         return(first);
  1165.     }
  1166.     if((t->flags&15)==UNION&&(*s=='{'||!noconst)){
  1167.         if(t->flags&UNCOMPLETE)
  1168.             {error(44);return(0);}
  1169.         if(*s=='{'){s++;killsp();bracket=1;} else bracket=0;
  1170.         first=initialization(t->exact->sl[0].styp,0);
  1171.         if(bracket)
  1172.             if(*s=='}'){s++;killsp();} else error(128);
  1173.         return(first);
  1174.     }
  1175.     tree2=tree=assignment_expression();
  1176.     if(!tree){error(45);return(0);}
  1177.     if(!type_expression(tree)){free_expression(tree); return(0);}
  1178.     tree=makepointer(tree);
  1179.     test_assignment(t,tree);
  1180.     if(!noconst){
  1181.     /*  nur Konstanten erlaubt (bei Arrays/Strukturen etc. oder static) */
  1182.         if(tree->flags!=CEXPR){
  1183.             while(tree->flags==CAST) tree=tree->left;
  1184.             if(tree->flags==ADDRESS||tree->flags==ADDRESSS||tree->flags==ADDRESSA){
  1185.                 gen_IC(tree,0,0);
  1186.                 if(!(tree->o.flags&VARADR)){
  1187.                 /*  hier fehlen noch viele Pruefungen   */
  1188.                     free_expression(tree);error(46);
  1189.                     return(0);
  1190.                 }
  1191.                 first=(struct const_list *)mymalloc(CLS);
  1192.                 first->next=first->other=0;
  1193.                 first->tree=tree;
  1194.                 killsp();
  1195.                 return(first);
  1196.             }else{
  1197.                 free_expression(tree);error(46);
  1198.                 return(0);
  1199.             }
  1200.         }
  1201.         first=(struct const_list *)mymalloc(CLS);
  1202.         first->next=first->other=0;
  1203.         first->tree=0;
  1204.         eval_constn(tree);
  1205.         tree->ntyp->flags=t->flags;
  1206.         insert_const(tree);
  1207.         first->val=tree->val;
  1208.         free_expression(tree2);
  1209.         killsp();
  1210.         return(first);
  1211.     }else{
  1212.     /*  auch anderes erlaubt    */
  1213.         first=(struct const_list *)mymalloc(CLS);
  1214.         first->next=first->other=0;
  1215.         first->tree=tree;
  1216.         killsp();
  1217.         return(first);
  1218.     }
  1219. }
  1220.  
  1221.  
  1222.  
  1223.